package com.okq.dao;

import com.okq.dao.base.Impl.BaseDao;
import com.okq.entity.OrderProduct;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 : 订单产品Dao
 * 作者 : zdl
 * 日期 : 2017/6/29 15:54
 */
@Repository
public class OrderProductDao extends BaseDao {
    public void saveOrderProduct(OrderProduct orderProduct) {
        super.save(orderProduct);
    }

    public void deleteOrderProduct(OrderProduct orderProduct) {
        super.delete(orderProduct);
    }

    public void updateOrderProduct(OrderProduct orderProduct) {
        super.update(orderProduct);
    }

    public void saveOrUpdateOrderProduct(OrderProduct orderProduct) {
        super.saveOrUpdate(orderProduct);
    }

    public OrderProduct getOrderProductById(String id) {
        return (OrderProduct) super.get(OrderProduct.class, id);
    }

    public List<OrderProduct> listAllOrderProduct() {
        String hql = "FROM OrderProduct WHERE isDelete = 0";
        return super.find(hql);
    }

    public Page<OrderProduct> pageByOrderProduct(Page<OrderProduct> page, OrderProduct orderProduct) {
        StringBuilder hqlSb = new StringBuilder();
        hqlSb.append("FROM OrderProduct op WHERE op.isDelete = 0 ");
        if (orderProduct != null) {
            Map<String, Object> param = new HashMap<>();
            String oderId = orderProduct.getOderId();
            if (StringUtils.isNotBlank(oderId)) {
                hqlSb.append(" AND op.oderId = ?! ");
                param.put("1", oderId);
            }
            hqlSb.append(" ORDER BY op.createTime DESC ");
            return super.findByPage(page, hqlSb.toString(), param);
        }
        return page;
    }

    /**
     * 根据产品编号查询订单产品表
     *
     * @param productCode 产品编号
     * @return OrderProduct
     */
    public OrderProduct getOrderProductByCode(String productCode) {
        if (StringUtils.isNotBlank(productCode)) {
            String hql = "FROM OrderProduct op WHERE op.isDelete = 0 AND op.products LIKE '%" + productCode + "%'";
            return (OrderProduct) super.get(hql);
        }
        return null;
    }
}
